﻿// Type: GalaSoft.MvvmLight.ObservableObject
// Assembly: GalaSoft.MvvmLight.SL5, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c114982fcf1a3a2e
// Assembly location: D:\DLL\GalaSoft.MvvmLight.SL5.dll

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Threading;

namespace GalaSoft.MvvmLight
{
  public class ObservableObject : INotifyPropertyChanged
  {
    private PropertyChangedEventHandler PropertyChanged;

    protected PropertyChangedEventHandler PropertyChangedHandler
    {
      get
      {
        return this.PropertyChanged;
      }
    }

    public event PropertyChangedEventHandler PropertyChanged
    {
      add
      {
        PropertyChangedEventHandler changedEventHandler = this.PropertyChanged;
        PropertyChangedEventHandler comparand;
        do
        {
          comparand = changedEventHandler;
          changedEventHandler = Interlocked.CompareExchange<PropertyChangedEventHandler>(ref this.PropertyChanged, comparand + value, comparand);
        }
        while (changedEventHandler != comparand);
      }
      remove
      {
        PropertyChangedEventHandler changedEventHandler = this.PropertyChanged;
        PropertyChangedEventHandler comparand;
        do
        {
          comparand = changedEventHandler;
          changedEventHandler = Interlocked.CompareExchange<PropertyChangedEventHandler>(ref this.PropertyChanged, comparand - value, comparand);
        }
        while (changedEventHandler != comparand);
      }
    }

    [DebuggerStepThrough]
    [Conditional("DEBUG")]
    public void VerifyPropertyName(string propertyName)
    {
      Type type = this.GetType();
      if (!string.IsNullOrEmpty(propertyName) && type.GetProperty(propertyName) == null)
        throw new ArgumentException("Property not found", propertyName);
    }

    protected virtual void RaisePropertyChanged(string propertyName)
    {
      PropertyChangedEventHandler changedEventHandler = this.PropertyChanged;
      if (changedEventHandler == null)
        return;
      changedEventHandler((object) this, new PropertyChangedEventArgs(propertyName));
    }

    protected virtual void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
    {
      if (propertyExpression == null)
        return;
      PropertyChangedEventHandler changedEventHandler = this.PropertyChanged;
      if (changedEventHandler == null)
        return;
      MemberExpression memberExpression = propertyExpression.Body as MemberExpression;
      changedEventHandler((object) this, new PropertyChangedEventArgs(memberExpression.Member.Name));
    }

    protected void Set<T>(Expression<Func<T>> propertyExpression, ref T field, T newValue)
    {
      if (EqualityComparer<T>.Default.Equals(field, newValue))
        return;
      field = newValue;
      this.RaisePropertyChanged<T>(propertyExpression);
    }

    protected void Set<T>(string propertyName, ref T field, T newValue)
    {
      if (EqualityComparer<T>.Default.Equals(field, newValue))
        return;
      field = newValue;
      this.RaisePropertyChanged(propertyName);
    }
  }
}
